package edu.bbu.bibliospring.backend.service.beans;

import edu.bbu.bibliospring.backend.model.Profile;
import edu.bbu.bibliospring.backend.repository.ProfileRepository;
import edu.bbu.bibliospring.backend.repository.exception.BibliospringRepositoryException;
import edu.bbu.bibliospring.backend.service.ProfileService;
import edu.bbu.bibliospring.backend.service.exception.BibliospringServiceException;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by anna on 6/12/14.
 */
@Stateless(name = "ProfileService", mappedName = "ejb/ProfileService")
@DependsOn({"ProfileRepository", "Profile"})
public class ProfileServiceBean implements ProfileService {

    private static final java.util.logging.Logger LOG = Logger.getLogger(ProfileService.class.getName());

    @EJB
    ProfileRepository profileRepository;

    @Override
    public List<Profile> getAll() throws BibliospringServiceException {
        try {
            LOG.info("ProfileService - getAll");
            return profileRepository.getAll();
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Profile repository - getAll()", e);
        }
    }

    @Override
    public Profile getById(Long id) throws BibliospringServiceException {
        try {
            LOG.info("ProfileService - getById");
            return profileRepository.getById(id);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Profile repository - getById()", e);
        }
    }

    @Override
    public Profile insertProfile(Profile profile) throws BibliospringServiceException {
        try {
            LOG.info("ProfileService - insertProfile");
            return profileRepository.save(profile);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Profile repository - insertProfile()", e);
        }
    }
}
